package com.promise.controller.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.promise.controller.Code;
import com.promise.controller.result.UserResult;
import com.promise.util.TokenUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ProjectInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        if ("OPTIONS".equals(request.getMethod())) {
            return true;
        }

//        String requestURI = request.getRequestURI();
//        if ("/prometheus/query".equals(requestURI) || "/prometheus/memory".equals(requestURI) || "/prometheus/disk".equals(requestURI) || "/prometheus/network".equals(requestURI) || "/prometheus/netstatwait".equals(requestURI) || "/prometheus/losspk".equals(requestURI) || "/prometheus/timeping".equals(requestURI) || "/dutys/add".equals(requestURI) || "/users/16".equals(requestURI) || "/dutys/delete/3".equals(requestURI) || "/dutys/update/3".equals(requestURI) || "/monitors/add".equals(requestURI) || "/monitors/delete/3".equals(requestURI)  || "/monitors/update/3".equals(requestURI) || "/monitors/all".equals(requestURI) || "/automates/delete/3".equals(requestURI) || "/warnings/delete/3".equals(requestURI) || "/faults/delete/3".equals(requestURI) || "/records/delete/3".equals(requestURI) || "/records/1/10".equals(requestURI) || "/users/1/10".equals(requestURI) || "/faults/1/10".equals(requestURI) || "/automates/1/10".equals(requestURI) || "/warnings/1/10".equals(requestURI) || "/warnings/add".equals(requestURI) || "/warnings/update/3".equals(requestURI) || "/automates/update/2".equals(requestURI) || "/records/update/2".equals(requestURI) || "/faults/update/2".equals(requestURI) || "/users/update/25".equals(requestURI) || "/users/restore/25".equals(requestURI) || "/warning/add".equals(requestURI) || "/automates/add".equals(requestURI) || "/records/add".equals(requestURI) || "/faults/add".equals(requestURI)) {
//            // 如果是 Prometheus 请求的路径或者查询磁盘使用率的路径，则放行
//            return true;
//        }

        // 获取请求头的token数据
        ObjectMapper objectMapper = new ObjectMapper();
        String token = request.getHeader("Authorization");
        response.setContentType("text/html;charset=utf-8");
        // 解析token数据
        if (token != null) {
            // 删除头部的 Bearer+空格
            token = token.substring(7);
            // 做出响应
            if (!TokenUtils.verify(token)) {
                response.getWriter().write(objectMapper.writeValueAsString(new UserResult(Code.ERROR_CODE,
                        null, "token无效")));
                return false;
            } else return true;
        } else {
            // 做出响应
            response.getWriter().write(objectMapper.writeValueAsString(new UserResult(Code.ERROR_CODE,
                    null, "没有权限")));
            return false;
        }
    }



}


